package bsf.baseservice.businessmq.systemruntime.log;

import bsf.base.CallBack;
import bsf.baseservice.businessmq.dal.tb_debuglog_dal;
import bsf.baseservice.businessmq.model.tb_debuglog_model;
import bsf.baseservice.businessmq.systemruntime.ConfigHelper;
import bsf.db.DbConn;
import bsf.util.DateUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

public class DebugHelper
{
	public static void timeWatch(int mqpathid, String mqpath, String methodname, CallBack.Func0 action)
	{
		Date startTime = new Date();
		action.invoke();
		long d = (new Date().getTime() - startTime.getTime())/1000;
		writeLine(mqpathid, mqpath, methodname, String.format("耗时:%1$s",d+""));
	}

	public static void writeLine(int mqpathid, String mqpath, String methodname, String info)
	{
		try
		{
			System.out.println(info + " " + DateUtil.toString(new Date()));
			if (!StringUtils.isEmpty(ConfigHelper.DebugMqpath) && ConfigHelper.DebugMqpath.toLowerCase().equals(mqpath.toLowerCase()))
			{
				if (!StringUtils.isEmpty(ConfigHelper.LogDBConnectString))
				{
					try(DbConn c = DbConn.createConn(ConfigHelper.LogDBConnectString))
					{
						tb_debuglog_dal dal = new tb_debuglog_dal();
						tb_debuglog_model t = new tb_debuglog_model();
						t.createtime=new Date();
						t.info=info;
						t.mqpath=mqpath;
						t.mqpathid=mqpathid;
						t.methodname=methodname;
						dal.add(c, t);
					}
				}
			}
		}
		catch (Exception exp)
		{
			/* 临时注释
			XXF.Log.ErrorLog.Write(String.format("BusinessMQ插入Debug信息时发生错误,mqpathid:%1$s,mqpath:%2$s,methodname:%3$s,info:%4$s",
					mqpathid, ConvertHelper.nullToStr(mqpath) ,  ConvertHelper.nullToStr(methodname),  ConvertHelper.nullToStr(info)), exp);
					*/
		}
	}
}